//
//  VisitorTableView.swift
//  微博
//
//  Created by qiukai on 16/4/6.
//  Copyright © 2016年 仇恺. All rights reserved.
//

import UIKit

class VisitorTableView: UIView {
    // MARK: - 外界调用板式的函数
    func setUpInfo(imageName: String?, title: String){
        messageLabel.text = title
        guard let imagen = imageName else{
            print(imageName)
            startAnim()
            return
        }
        iconView.image = UIImage(named: imagen)
        homeIconView.hidden = true
        maskIconView.hidden = true
    }
    // MARK: - 动画
    private func startAnim(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
    
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        
        anim.removedOnCompletion = false
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    // MARK: - 构造函数
    //纯代码开发调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    //用sb创建必定调用
    required init?(coder aDecoder: NSCoder) {
        //若用sb创建经过下方带吗必定崩溃
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        setUpUI()
    }
    // MARK: - 懒加载控件
        /// 图标
    private lazy var iconView = UIImageView(imageNamed: "visitordiscover_feed_image_smallicon")
        /// 遮罩图片
    private lazy var maskIconView = UIImageView(imageNamed: "visitordiscover_feed_mask_smallicon")
        /// 小房子
    private lazy var homeIconView = UIImageView(imageNamed: "visitordiscover_feed_image_house")
        /// 消息文字
    private lazy var messageLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")

        /// 注册按钮
    lazy var registerButton: UIButton = UIButton(imageName: "common_button_white_disable", Color: UIColor.orangeColor(), title: "注册")

        /// 登陆按钮
    lazy var loginButton: UIButton = UIButton(imageName: "common_button_white_disable", Color: UIColor.darkGrayColor(), title: "登陆")
    
}

extension VisitorTableView{
    // MARK: - 布局
    /// 布局
    private func setUpUI(){
        //添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        
        //给空间布局
        //1.图标
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        //2. 小房子
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //3. 消息文字
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //登陆按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        //遮盖
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .Bottom, relatedBy: .Equal, toItem: loginButton, attribute: .Bottom, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1.0, constant: 0))
        
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
    
}